package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BaseException;
import cn.wolfcode.car.common.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.Style;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    StatementItemMapper statementItemMapper;
    @Autowired
    private IAppointmentService appointmentService;


    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        Statement statementObj = new Statement();
        statementObj.setCustomerName(statement.getCustomerName());
        statementObj.setCustomerPhone(statement.getCustomerPhone());
        statementObj.setActualArrivalTime(statement.getActualArrivalTime());
        statementObj.setLicensePlate(statement.getLicensePlate());
        statementObj.setCarSeries(statement.getCarSeries());
        statementObj.setServiceType(statement.getServiceType());
        statementObj.setInfo(statement.getInfo());

        statementMapper.insert(statementObj);
    }


    @Override
    public void update(Statement statement) {
        Statement newObj = this.get(statement.getId());
        //只有处于消费中的预约单才可以进行修改
        if(Statement.STATUS_PAID.equals(newObj.getStatus())){
            throw new BusinessException("已经支付的结算单不能修改");
        }
        newObj.setCustomerName(statement.getCustomerName());
        newObj.setCustomerPhone(statement.getCustomerPhone());
        newObj.setActualArrivalTime(statement.getActualArrivalTime());
        newObj.setLicensePlate(statement.getLicensePlate());
        newObj.setCarSeries(statement.getCarSeries());
        newObj.setServiceType(statement.getServiceType());
        newObj.setInfo(statement.getInfo());

        statementMapper.updateByPrimaryKey(newObj);
    }

    @Override
    @Transactional
    public void deleteBatch(Long id) {
        Statement statement = this.get(id);
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BaseException("非法操作");
        }
        //删除结算单
        statementMapper.deleteByPrimaryKey(id);
        //删除结算单明细
        statementItemService.deleteByStatementId(id);
        if(statement.getAppointmentId() != null) {
            //修改预约单状态
            appointmentService.changeStatus(statement.getAppointmentId(), Appointment.STATUS_ARRIVAL);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateAmount(Long statementId, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal disCountPrice) {
        statementMapper.updateAmount(statementId,totalAmount,totalQuantity,disCountPrice);
    }

    @Override
    public void pay(Long statementId, Long userId, Integer status) {
        statementMapper.pay(statementId,userId,status);
    }

    @Override
    public void saveForAppointment(Statement statement) {
        statementMapper.insert(statement);
    }

    @Override
    public Statement getByAppointmentId(Long appointmentId) {
        return statementMapper.getByAppointmentId(appointmentId);
    }

    @Override
    public Workbook export(Long statementId) {
        //查询所有权限集合,封装Workbook
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        StatementItem item = statementItemMapper.selectByPrimaryKey(statementId);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format = dateFormat.format(statement.getActualArrivalTime());
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("客户消费表");
        //定义标题头
        Row titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("客户姓名");
        titleRow.createCell(1).setCellValue("联系方式");
        titleRow.createCell(2).setCellValue("车牌号码");
        titleRow.createCell(3).setCellValue("汽车类型");
        titleRow.createCell(4).setCellValue("服务类型");
        titleRow.createCell(5).setCellValue("到店时间");
        titleRow.createCell(6).setCellValue("总消费金额");
        titleRow.createCell(7).setCellValue("实付价格");
        titleRow.createCell(8).setCellValue("优惠价格");
        titleRow.createCell(9).setCellValue("服务项明细名称");
        titleRow.createCell(10).setCellValue("服务价格");
        titleRow.createCell(11).setCellValue("数量");
        //封装数据
            Row row = sheet.createRow(1);
            row.createCell(0).setCellValue(statement.getCustomerName());
            row.createCell(1).setCellValue(statement.getCustomerPhone());
            row.createCell(2).setCellValue(statement.getLicensePlate());
            row.createCell(3).setCellValue(statement.getCarSeries());
            row.createCell(4).setCellValue(statement.getServiceType());
            row.createCell(5).setCellValue(format);
            row.createCell(6).setCellValue(statement.getTotalAmount().toString());
            row.createCell(7).setCellValue((statement.getTotalAmount().subtract(statement.getDiscountAmount())).toString());
            row.createCell(8).setCellValue(statement.getDiscountAmount().toString());
            row.createCell(9).setCellValue(item.getItemName());
            row.createCell(10).setCellValue(item.getItemPrice().toString());
            row.createCell(11).setCellValue(item.getItemQuantity().toString());

        return wb;
    }
}

